<template>
  <div>
    <el-upload
      list-type="picture-card"
      :limit="1"
      action="#"
      :on-preview="preview"
      :file-list="fileList"
      :on-remove="handleRemove"
      :on-change="changeFile"
      :before-upload="beforeUpload"
      :http-request="upload"
      :class="{ disabled: fileComputed }"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-dialog title="图片" :visible.sync="showDialog">
      <img :src="imgUrl" style="width:100%" alt="" />
    </el-dialog>
    <el-progress
      v-if="showPercent"
      style="width: 180px"
      :percentage="percent"
    />
  </div>
</template>
<script>
import COS from "cos-js-sdk-v5";
var cos = new COS({
  SecretId: "AKID94lLgpVYpY0BJCJHC1kNBtRukNOOKmDS", // 身份识别 ID
  SecretKey: "UkmeKz0qmSs45vmAKE7qNhtfEahedOxi" // 身份密钥
});
export default {
  data() {
    return {
      fileList: [
        {
          url:
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.08087.cc%2Fuploads%2F20191221%2F19%2F1576927577-ezdtLhXcng.jpg&refer=http%3A%2F%2Fimg.08087.cc&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1616815165&t=babec993fe61e02a2b9e5182c4da5c8b"
        }
      ],
      showDialog: false,
      imgUrl: "",
      currenFileUid: null,
      showPercent: false,
      percent: 0
    };
  },
  computed: {
    // 设定一个计算属性 判断是否已经上传完了一张
    fileComputed() {
      return this.fileList.length === 1;
    }
  },
  methods: {
    preview(file) {
      //   console.log(file);
      this.imgUrl = file.url;
      this.showDialog = true;
    },
    handleRemove(file) {
      this.fileList = this.fileList.filter(item => file.uid !== item.uid);
    },
    changeFile(file, fileList) {
      console.log(file);
      console.log(fileList);
      this.fileList = fileList.map(item => item);
    },
    beforeUpload(file) {
      // 要开始做文件上传的检查了
      // 文件类型 文件大小
      const types = ["image/jpeg", "image/gif", "image/bmp", "image/png"];
      if (!types.includes(file.type)) {
        this.$message.error("上传图片只能是 JPG、GIF、BMP、PNG 格式!");
        return false;
      }
      //  检查大小
      const maxSize = 5 * 1024 * 1024;
      if (maxSize < file.size) {
        this.$message.error("图片大小最大不能超过5M");
        return false;
      }
      this.currenFileUid = file.uid;
      this.showPercent = true;
      return true;
    },
    upload(params) {
      if (params.file) {
        cos.putObject(
          {
            Bucket: "router-z-1305062448", // 存储桶
            Region: "ap-nanjing", // 地域
            Key: params.file.name, // 文件名
            Body: params.file, // 要上传的文件对象
            StorageClass: "STANDARD",
            onProgress: params => {
              this.percent = params.percent * 100;
            } // 上传的模式类型 直接默认 标准模式即可
            // 上传到腾讯云 =》 哪个存储桶 哪个地域的存储桶 文件  格式  名称 回调
          },
          (err, data) => {
            // data返回数据之后 应该如何处理
            console.log(err || data);
            if (!err && data.statusCode === 200) {
              this.fileList = this.fileList.map(item => {
                if (item.uid === this.currenFileUid) {
                  return { url: "http://" + data.Location, upload: true };
                }
                return item;
              });
              setTimeout(() => {
                this.showPercent = false;
                this.percent = 0;
              }, 1000);
            }
          }
        );
      }
    }
  }
};
</script>

<style lang="scss">
.disabled .el-upload--picture-card {
  display: none;
}
</style>
